package cn.topcodes.tcsf.wechat.web.controller;

import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.wechat.domain.entity.WechatVideo;
import cn.topcodes.tcsf.wechat.domain.form.WechatVoiceForm;
import cn.topcodes.tcsf.wechat.domain.vo.WechatVideoVo;
import cn.topcodes.tcsf.wechat.service.WechatFileService;
import cn.topcodes.tcsf.wechat.service.WechatMpService;
import cn.topcodes.tcsf.wechat.service.WechatVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author xw
 * @date 2017-12-22 15:16:15
 */
@Controller
@RequestMapping("/admin/wechat/video")
public class WechatVideoController extends AdminController<WechatVideo, WechatVideoVo, WechatVoiceForm> {

    @Autowired
    private WechatMpService wechatMpService;

    @Autowired
    private WechatVideoService wechatVideoService;

    @Autowired
    private WechatFileService wechatFileService;

    @Override
    protected String getPageFolder() {
        return "wechat/admin/material/video";
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public String upload(WechatVideo entity, MultipartFile file) {
        String name = file.getOriginalFilename();
        String videoSavePath = wechatFileService.getVideoStoragePath();
        File videoFile = new File(videoSavePath);
        if (!videoFile.exists()) {
            videoFile.mkdirs();
        }
        videoFile = new File(videoSavePath, name);

        try {
            if (!videoFile.exists()) {
                videoFile.createNewFile();
            } else {
                videoFile =
                        wechatFileService.createFile(
                                videoSavePath, wechatFileService.getSuffix(name));
            }
            file.transferTo(videoFile);
            String mpId = wechatMpService.getMpId(entity.getWechatMpId());
            wechatVideoService.upload(mpId, entity, videoFile);
        } catch (Exception e) {
            if (videoFile.exists()) {
                videoFile.delete();
            }
            e.printStackTrace();
        }
        return "redirect:/admin/wechat/video";
    }

    @RequestMapping(value = "save")
    public String save(WechatVideo video) {
        wechatVideoService.update(video);
        return "redirect:/admin/wechat/video";
    }

    @RequestMapping(value = "set-cover", method = RequestMethod.POST)
    @ResponseBody
    public ApiResponseEntity setCover(@RequestParam Long id, @RequestParam String data) {
        WechatVideo video = wechatVideoService.setCover(id, data);
        Assert.notNull(video, "This entity does not exist");
        return ApiResponseEntity.buildOK(video.getCoverUrl());
    }

    @RequestMapping(value = "download")
    public void download(@RequestParam Long id, HttpServletResponse response) throws IOException {
        WechatVideo video = wechatVideoService.findOne(id);
        if (video != null) {
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(wechatFileService.getNameByUrl(video.getUrl(), "/"), "UTF-8"));
            response.setHeader("Connection", "close");
            response.setHeader("Content-Type", "application/octet-stream");
            wechatFileService.responseFileByName(
                    WechatFileService.FILE_TYPE_VIDEO, wechatFileService.getNameByUrl(video.getUrl(), "/"), response.getOutputStream());
        }
    }
}
